package userInterface;

import java.awt.Color;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;


public class FileWriter465
{
	private static String formatColor(Color color)
	{
		return color.getRed() + " " + color.getGreen() + " " + color.getBlue();
	}
	
	private static String formatPoint(Point p)
	{
		return p.X + " " + p.Y;
	}
	
	private static void writeLine(PrintWriter out, Line l)
	{
		out.write("L " + 
			formatPoint(l.getStartPoint()) + " " +
			formatPoint(l.getEndPoint()) + " " +
			l.getThickness() + " " +
			formatColor(l.getColor()));
	}
	
	private static void writeRectangle(PrintWriter out, Rectangle r)
	{
		out.write("R " + 
				formatPoint(r.getTopLeft()) + " " +
				r.getWidth() + " " +
				r.getHeight() + " " +
				r.getThickness() + " " +
				formatColor(r.getColor()));		
	}

	private static void writeEllipse(PrintWriter out, Ellipse e)
	{
		out.write("E " + 
				formatPoint(e.getTopLeft()) + " " +
				e.getWidth() + " " +
				e.getHeight() + " " +
				e.getThickness() + " " +
				formatColor(e.getColor()));				
	}

	private static void writeCurve(PrintWriter out, Curve c)
	{
		out.write("B " + 
				formatPoint(c.getStart()) + " " +
				formatPoint(c.getCtl1()) + " " +
				formatPoint(c.getCtl2()) + " " +
				formatPoint(c.getEnd()) + " " +
				c.getThickness() + " " +
				formatColor(c.getColor()));						
	}

	private static void writeText(PrintWriter out, Text t)
	{
		out.write("T " + 
				formatPoint(t.getLocation()) + " " +
				t.getSize() + " " +
				"\"" + t.getText() + "\" " +
				formatColor(t.getColor()));								
	}

	public static void writeShape(PrintWriter out, DrawObject obj)
	{

		switch (obj.getShape())
		{
		case LINE:
			writeLine(out, (Line) obj);
			break;
		case RECTANGLE:
			writeRectangle(out, (Rectangle) obj);
			break;
		case ELLIPSE:
			writeEllipse(out, (Ellipse) obj);
			break;
		case CURVE:
			writeCurve(out, (Curve) obj);
			break;
		case TEXT:
			writeText(out, (Text) obj);
			break;
		case NONE:
			break;
		}
		
	}
	
	public static void writeFile(String filename, ArrayList<DrawObject> objects)
	{
		try
		{
			FileWriter outFile = new FileWriter(filename);
			PrintWriter out = new PrintWriter(outFile);

			for (int i = 0; i < objects.size(); i++)
			{
				writeShape(out, objects.get(i));
				out.write("\n");
			}
			
			out.close();
			outFile.close();
			
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}

